package com.example.demo.service;

import com.example.demo.bean.LoginFro;
import com.example.demo.bean.User;
import com.example.demo.dao.UserDao;
import com.example.demo.exception.CodeMsg;
import com.example.demo.exception.GlobalException;
import com.example.demo.redis.RedisServer;
import com.example.demo.redisPrefix.UserPrefixAndExpireSecond;
import com.example.demo.util.MD5Util;
import com.example.demo.util.UUIDUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.provider.MD5;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@Service
public class UserService {
    @Autowired
    RedisServer redisServer;
    @Autowired
    UserDao userDao;


    public String login(HttpServletResponse response,LoginFro loginFro){
        if (loginFro==null){
            throw new GlobalException(CodeMsg.BIND_ERROR);
        }

        String mobile = loginFro.getMobile();
        String password = loginFro.getPassword();

        User userStorage = testLogin(mobile);

        //将登陆的password进行二次加密与数据库的相验证
        String password_md5 = MD5Util.inputPassToFormPass(password);
        String storagePassword = userStorage.getPassword();
        if (storagePassword.equals(password_md5)==false){
            throw new GlobalException(CodeMsg.PASSWORD_ERROR);
        }
        //生成token
        String token = UUIDUtil.getUUID();
        //存入redis
        addCookie(response,token,userStorage);
        return token;

    }

    public User testLogin(String mobile){
        //在redis中先判断有无对应的user
        User user = null;
        try {
            user = redisServer.get(UserPrefixAndExpireSecond.re_loginUser, mobile, User.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        System.out.println(user);
        if (user!=null){
            //说明在数据库中查找过对应的mobile对应的信息了，只是还没有验证密码
            return user;
        }

        //在数据库中查找
        User userLogin = userDao.findUser(Long.parseLong(mobile));
        if (userLogin==null){
            throw new GlobalException(CodeMsg.MOBILE_NOT_EXIST);
        }

        System.out.println(userLogin);
        //能在数据库中查到对应的mobile则可以进行存入redis,因为后续继续登录验证就不用在查找数据库了
        String set=null;
        try {
            set = redisServer.set(UserPrefixAndExpireSecond.re_loginUser, mobile, userLogin);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println(set);
        System.out.println("1111");
        return userLogin;
    }

    //生成token为key,以user为value来模拟session
    public void addCookie(HttpServletResponse response,String token,User user) {
        System.out.println(token);
        String set=null;
        try {
            set = redisServer.set(UserPrefixAndExpireSecond.re_token, token, user);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println(set);
        //存入浏览器中
        Cookie cookie=new Cookie("token",token);
        //设置cookie的有效期(一小时)
        cookie.setMaxAge(UserPrefixAndExpireSecond.TOKEN_EXPIRE);
        cookie.setPath("/");
        response.addCookie(cookie);
    }
}
